package com.twu.biblioteca;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Book
{
    String name;
    int id;
    boolean isAvailable;
    String currentUser;

    protected static Book[] list=seed();
    protected static int total=list.length;

    Book(String bookName, int idNumber)
    {
     name=bookName;
     id=idNumber;
     isAvailable=true;
     currentUser=null;
    }

    public boolean equals(Object obj)
    {
        if(obj instanceof Book)
        {
            Book second=(Book)obj;
            return this.name.equals(second.name)&&this.id==second.id;
        }
        return false;
    }

    protected static Book findById(int id)
    {
        for(int i=0;i<total;i++)
        {
            if(list[i].id==id)
                return list[i];
        }
        return null;
    }

    protected static Book findByName(String name)
    {
        for(int i=0;i<total;i++)
        {
            if(list[i].name.equals(name))
                return list[i];
        }
        return null;
    }

    protected boolean isAvailable()
    {
        return this.isAvailable;
    }

    protected void reserve(String userId)
    {
        this.isAvailable=false;
        this.currentUser=userId;
    }

    protected void unreserve()
    {
        this.isAvailable=true;
        this.currentUser=null;
    }

    protected static void reserveMenu()
    {
        System.out.print("*********************************************************\n");
        System.out.print("Reserve Book Menu:-\nEnter number to reserve by ID, book name to reserve by name: ");
        String input="";
        int number;
        Book tmp=null;
        try
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            input = br.readLine();
            number=Integer.parseInt(input);
            tmp=Book.findById(number);
        }
        catch(NumberFormatException e)
        {
            tmp=Book.findByName(input);
        }
        catch(Exception e)
        {
            System.out.println("Error Occured: " + e.toString());
        }
        if(tmp==null)
            System.out.println("INVALID ENTRY!!!");
        else
        {
            if(tmp.isAvailable())
            {
                String userName=BibliotecaApp.currentUser.libraryNo;
                tmp.reserve(userName);
                System.out.println("Thank You! Enjoy the book.");
            }
            else
                System.out.println("Sorry we don't have that book yet.");
        }
    }

    private void display()
    {
        System.out.println(this.id + "\t" + this.name + "\t\t" +(isAvailable()? "Available" : "Not Available") + "\t");
    }

    private static void displayHeader()
    {
        System.out.print("*********************************************************\n");
        System.out.print("LIST OF BOOKS\n\n");
        System.out.print("ID\tNAME\t\tAVAILABILITY\n");
    }

    protected static void displayAll()
    {
        displayHeader();
        for(int i=0;i<total;i++)
        {
            list[i].display();
        }
    }

    private static Book[] seed()
    {
        return new Book[]{new Book("Orion",1),new Book("Hydra",2),new Book("Chimera",3)};
    }
}
